//
//  WBVisitorView.swift
//  CFZWeiBo
//
//  Created by Cedric Zheng on 26/11/2016.
//  Copyright © 2016 Cedric Zheng. All rights reserved.
//

import UIKit

protocol WBVisitorViewDelegate: NSObjectProtocol {
    func beginlogin()
}

class WBVisitorView: UIView {
    
    weak var delegate: WBVisitorViewDelegate?

    var visitorInfo: [String: Any]?
    
    lazy var iconImage:UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    
    /// A spinnig image on the background
    lazy var circleImage: UIImageView = UIImageView(image:#imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    
    /// A mask image, it will cover part of circleImage
    lazy var maskImage: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    
    lazy var textLable: UILabel = UILabel(title: "", fontSize: 13, alignment: .center)
    
    lazy var registerButton: UIButton = UIButton(title: "注册", fontSize: 15, color: globalColor, bgImageName: "common_button_white_disable")
    
    lazy var loginButton: UIButton = UIButton(title: "登录", fontSize: 15, color: UIColor.darkGray, bgImageName: "common_button_white_disable", target: self, selector: #selector(loginButtonTapped))
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: - Setup methods
extension WBVisitorView {
    
    fileprivate func setupUI () {
        self.backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)
        
        addSubview(circleImage)
        addSubview(maskImage)
        addSubview(iconImage)
        addSubview(textLable)
        addSubview(registerButton)
        addSubview(loginButton)
        
        let ruler = screenHeight*0.1
        
        iconImage.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-ruler)
        }
        
        circleImage.snp.makeConstraints { (make) in
            make.center.equalTo(iconImage)
        }
        
        maskImage.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-ruler)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
        }
        
        textLable.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(ruler)
            make.right.equalToSuperview().offset(-ruler)
            make.centerY.equalTo(iconImage).offset(ruler*1.5)
        }
        
        registerButton.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview().offset(-ruler)
            make.centerY.equalToSuperview().offset(ruler*2)
            make.size.equalTo(CGSize(width: ruler, height: ruler*0.5))
        }
        
        loginButton.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview().offset(ruler)
            make.centerY.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }
        
        addAnimation()
    }
    
    func setVisitorView (isAnimation: Bool, image: UIImage, title: String) {
        circleImage.isHidden = !isAnimation
        iconImage.image = image
        textLable.text = title
    }
}

// MARK: - Private methods
extension WBVisitorView {
    
    fileprivate func addAnimation () {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = 15
        animation.isRemovedOnCompletion = false
        
        circleImage.layer.add(animation, forKey: nil)
    }
    
}

extension WBVisitorView {
    @objc fileprivate func loginButtonTapped() {
        delegate?.beginlogin()
    }
}
